<?php
/**
 * Created by PhpStorm.
 * User: 93195
 * Date: 2018/6/25
 * Time: 10:11
 */
class InviteRewardLogic extends Model
{
    private $customerId = null;

    public function __construct()
    {
        $this->customerId = Session::$user['customerId'];
    }

    /**获取邀请应该获得的积分
     * @param string $type 'new'-邀请新用户、'subscribe'-关注公众号、'register'-注册
     * @return array
     */
    public function getInvitePoint()
    {
        $cid = Session::$user['customerId'];
        $customerId = $this->customerId;
        Session::switchCustomer(0);
        $inviteRewardModel = M('invite_reward');
        $pointSchemeModel = M('module_point_scheme');
        Session::switchCustomer($cid);

        $inviteRewardWhere['status'] = 1;
        $inviteRewardWhere['cid'] = $cid;
        $inviteRewardWhere['deleted'] = 0;
        $inviteRewardWhere['startTime'] = ['elt', date('Y-m-d', time())];
        $inviteRewardWhere['endTime'] = ['egt', date('Y-m-d', time())];
        $inviteRewards = $inviteRewardModel->where($inviteRewardWhere)->select();

        $results = [];
        foreach ($inviteRewards as $inviteReward) {
            $pointSchemeId = $inviteReward['pointSchemeId'];
            $pointScheme = $pointSchemeModel->where(['id' => $pointSchemeId, 'status' => 1, 'deleted' => 0])->find();
            if (!$pointScheme) $pointScheme = $pointSchemeModel->where(['cid' => $customerId, 'default' => 1, 'deleted' => 0, 'status' => 1])->find();
            $properties = $pointScheme['property'];
            $arrProperty = json_decode($properties, true);

            if ($arrProperty) {
                foreach ($arrProperty as $property) {
                    if ($property['type'] == PROPERTY_TYPE_INVITE && $property['status'] == 1) {//积分属性是购买商品获得积分方式
                        $keyProperty = $property;
                    }
                }
            }

            if (isset($keyProperty)) {
//                $result = [
//                    'newUserPoint' => $keyProperty['newUserPoint'],
//                    'newMiPoint' => $keyProperty['newMiPoint'],
//                    'newPreMiPoint' => $keyProperty['newPreMiPoint'],
//                    'newPrePreMiPoint' => $keyProperty['newPrePreMiPoint'],
//                    'newRecommenderPoint' => $keyProperty['newRecommenderPoint'],
//                    'noInviteNewReward' => $keyProperty['noInviteNewReward'],
//                    'subscribeUserPoint' => $keyProperty['subscribeUserPoint'],
//                    'subscribeMiPoint' => $keyProperty['subscribeMiPoint'],
//                    'subscribePreMiPoint' => $keyProperty['subscribePreMiPoint'],
//                    'subscribePrePreMiPoint' => $keyProperty['subscribePrePreMiPoint'],
//                    'subscribeRecommenderPoint' => $keyProperty['subscribeRecommenderPoint'],
//                    'noInviteSubscribeReward' => $keyProperty['noInviteSubscribeReward'],
//                    'registerUserPoint' => $keyProperty['registerUserPoint'],
//                    'registerMiPoint' => $keyProperty['registerMiPoint'],
//                    'registerPreMiPoint' => $keyProperty['registerPreMiPoint'],
//                    'registerPrePreMiPoint' => $keyProperty['registerPrePreMiPoint'],
//                    'registerRecommenderPoint' => $keyProperty['registerRecommenderPoint'],
//                    'noInviteRegisterReward' => $keyProperty['noInviteRegisterReward']
//                ];
                $result = $keyProperty;
            }
            $result['inviteRewardId'] = $inviteReward['id'];
            $result['pointSchemeId'] = $pointScheme['id'];
            $result['pointSchemeName'] = $pointScheme['name'];
            array_push($results, $result);
        }
        return $results;
    }


    public function addInviteRecord(
        $userId,
        $inviteRewardId,
        $inviteUserId,
        $type,
        $newUserPoint,
        $isSubscribe,
        $subscribePoint,
        $isRegister,
        $registerPoint,
        $pointConfig,
        $module = null,
        $moduleId = null,
        $id = ''
    )
    {
        $cid = Session::$user['customerId'];
        $customerId = $this->customerId;
        Session::switchCustomer(0);
        $inviteRecordModel = M('invite_record');
        Session::switchCustomer($cid);

        $data = [
            'cid' => $customerId,
            'userId' => $userId,
            'inviteRewardId' => $inviteRewardId,
            'inviteUserId' => $inviteUserId,
            'time' => date('Y-m-d H:i:s', time()),
            'type' => $type,
            'newUserPoint' => $newUserPoint,
            'isSubscribe' => $isSubscribe,
            'subscribePoint' => $subscribePoint,
            'isRegister' => $isRegister,
            'registerPoint' => $registerPoint,
            'pointConfig' => $pointConfig,
            'module' => $module,
            'moduleId' => $moduleId
        ];

        if ($id) {
            $inviteRecordModel->where(['id' => $id])->save($data);
            return $id;
        } else {
            return $inviteRecordModel->add($data);
        }
    }

    public function ifSubscribe($userId)
    {
        $cid = Session::$user['customerId'];
        Session::switchCustomer($this->customerId);
        $userModel = M('user');
        Session::switchCustomer($cid);

        $status = $userModel->where(['id' => $userId])->getField('status');
        if ($status == 'subscribe') {
            return true;
        } elseif ($status == 'unsubscribe') {
            return false;
        } else {
            return false;
        }
    }

    public function getInviteRecord($where, $type = 'select', $order = 'id desc')
    {
        $cid = Session::$user['customerId'];
        $customerId = $this->customerId;
        Session::switchCustomer(0);
        $inviteRecordModel = M('invite_record');
        Session::switchCustomer($cid);

        if ($type == 'select') {
            $result = $inviteRecordModel->where($where)->order($order)->select();
        } elseif ($type == 'find') {
            $result = $inviteRecordModel->where($where)->order($order)->find();
        }

        return $result;
    }

    public function updateInviteRecord($where, $data)
    {
        $cid = Session::$user['customerId'];
        $customerId = $this->customerId;
        Session::switchCustomer(0);
        $inviteRecordModel = M('invite_record');
        Session::switchCustomer($cid);

        return $result = $inviteRecordModel->where($where)->save($data);
    }
}